<template>
  <div class="continue">
    <van-notice-bar wrapable :scrollable="false" text="您还有未填完的信息，请点击进入填写。" />
    <div class="wrap">
      <van-cell-group>
        <van-cell title="住院号" :value="docAdmiNote.admissionNum || '-'" />
        <van-cell title="患者姓名" :value="docAdmiNote.patName || '-'" />
        <van-cell title="性别" :value="docAdmiNote.patSex || '-'" />
        <van-cell title="身份证号" :value="docAdmiNote.patIdNo || '-'" />
        <van-cell title="出生日期" :value="docAdmiNote.dateOfBirth || '-'" />
        <van-cell title="收治科室" :value="docAdmiNote.admissionDeptName || '-'" />
        <van-cell title="住院按金" :value="docAdmiNote.admMoney || '-'" />
        <van-cell title="入院情况" :value="docAdmiNote.patAdmCondition || '-'" />
        <van-cell title="入院途径" :value="docAdmiNote.patientClass || '-'" />
        <van-cell title="入院方式" :value="docAdmiNote.admissionWay || '-'" />
        <van-cell title="门（急）诊诊断" :value="docAdmiNote.diagDesc || '-'" />
      </van-cell-group>
    </div>
    <div style="padding: 32px 15px">
      <van-button @click="goStartRegister" style="height: 40px" color="#40BB90" type="primary" block
        >继续填写</van-button
      >
    </div>
  </div>
</template>

<script lang="ts" setup>
import { IgetDetailByNoticeSerialNo } from '@/typings/getDetailByNoticeSerialNo';
import { commonTypes } from '@/typings/commonTypes';
import { IqueryDictionaries } from '@/typings/queryDictionaries';
import { isEmpty } from '@/utils/common';

import {
  getProvCityCounty,
  queryDictionaries,
  getDetailByNoticeSerialNo,
} from '@/services/inHospitalApi';
import { useTo } from '@/utils/useTo';

import { Cell, CellGroup, Dialog, NoticeBar, Toast } from 'vant';

//因为这里用了script的setup方式就用不到 defineComponent 这个定义组件的api了
import {
  useSlots,
  useAttrs,
  defineProps,
  computed,
  onMounted,
  watchEffect,
  ref,
  reactive,
} from 'vue';

import { useStore } from 'vuex';
const store = useStore<commonTypes.IState>();

// 跳路由用
import router from '@/router/index';
// 获取路由参数
import { useRoute } from 'vue-router';
const route = useRoute();
// const slots = useSlots();
// const attrs = useAttrs();

//import HelloWorld from '@/components/HelloWorld.vue'; // @ is an alias to /src

const value = ref(3);

const formPam = reactive<IgetDetailByNoticeSerialNo.IRootObject>({
  isConfirm: null,
  busObj: {
    docAdmiNote: {
      patHospitalVisitId: '1',
      patientClass: '急诊',
      admissionDeptName: '产科',
      admMoney: '3000.00',
      patSex: '男',
      patName: '谭绍鹏',
      diagDesc: ';呕吐',
      dateOfBirth: '2012-10-27',
      admissionWay: '步行',
      patAdmCondition: '危',
      patHospitalId: '0366742',
      noticeSerialNo: '201907220101',
      wardName: '产科护理单元',
      admissionNum: 'P106645',
      patIdNo: '441284198410270810',
    },
    nurseAdmiNote: {
      lastConfirmTime: '2017-07-12 17:05:58',
      bedNo: '101',
      handleInHospitalTime: '2017-07-12 17:05:58',
    },
    patAdmiNote: {
      birthday: '2012-10-27 00:00:00.0',
      registerCity: '广州市',
      bloodPressure: null,
      nation: {
        code: null,
        name: '汉族',
      },
      idCardNo: '441284198410270810',
      registerCounty: '天河区',
      phoneNo: '13800138000',
      operator: 'CR',
      relation: {
        code: '3',
        name: null,
      },
      contactAddressProv: '广东省',
      times: null,
      patSource: {
        code: '1',
        name: null,
      },
      registerStreet: '棠下街道华景北路113号',
      marriage: {
        code: null,
        name: '未婚',
      },
      contactAddressCity: '广州市',
      identity: {
        code: null,
        name: '广州医保',
      },
      registerProv: '广东省',
      contactAddress: '广东省广州市天河区棠下街道华景北路113号',
      residence: {
        code: null,
        name: '广东省广州市天河区棠下街道@',
      },
      height: null,
      profession: {
        code: '13',
        name: null,
      },
      residenceStreet: '华景北路113号',
      patientName: '谭绍鹏',
      checkedFlag: '1',
      contactName: '谭绍鹏',
      sex: {
        code: null,
        name: '男',
      },
      weight: null,
      birthCity: {
        code: null,
        name: '广州市',
      },
      contactAddressStreet: '棠下街道华景北路113号',
      workAddress: '南医三信息科',
      noticeSerialNo: '201907220101',
      birthProv: {
        code: null,
        name: '广东省',
      },
      residencePhone: '13800138000',
      nationality: {
        code: 'CN',
        name: null,
      },
      birthCounty: {
        code: null,
        name: '天河区',
      },
      animalHeat: null,
      contactAddressCounty: '天河区',
      pulse: null,
      nativePlace: {
        code: '120111',
        name: null,
      },
      workZipCode: '000000',
      residenceZip: '000000',
      contactPhone: '13700137000',
      register: '广东省广州市天河区棠下街道华景北路113号',
    },
  },
});

const dictionaries: IqueryDictionaries.IBusObj = {
  sex: [
    {
      code: '0',
      name: '未知',
    },
    {
      code: '1',
      name: '男',
    },
    {
      code: '2',
      name: '女',
    },
    {
      code: '9',
      name: '未定',
    },
  ],
  nationality: [
    {
      code: '9',
      name: '英国',
    },
    {
      code: '191',
      name: '法国',
    },
  ],
  nation: [
    {
      code: '9',
      name: '汉族',
    },
    {
      code: '91',
      name: '壮族',
    },
  ],
  marriage: [
    {
      code: '1',
      name: '未婚',
    },
    {
      code: '2',
      name: '已婚',
    },
    {
      code: '3',
      name: '丧偶',
    },
    {
      code: '4',
      name: '离婚',
    },
    {
      code: '9',
      name: '其他',
    },
  ],
  profession: [
    {
      code: '11',
      name: '国家公务员',
    },
    {
      code: '13',
      name: '专业技术人员',
    },
    {
      code: '17',
      name: '职员',
    },
    {
      code: '21',
      name: '企业管理人员',
    },
    {
      code: '24',
      name: '工人',
    },
    {
      code: '27',
      name: '农民',
    },
    {
      code: '31',
      name: '学生',
    },
    {
      code: '37',
      name: '现役军人',
    },
    {
      code: '51',
      name: '自由职业者',
    },
    {
      code: '54',
      name: '个体经营者',
    },
    {
      code: '70',
      name: '无业人员',
    },
    {
      code: '80',
      name: '退(离)休人员',
    },
    {
      code: '90',
      name: '其他',
    },
  ],
  relation: [
    {
      code: '1',
      name: '配偶',
    },
    {
      code: '2',
      name: '子',
    },
    {
      code: '3',
      name: '女',
    },
    {
      code: '4',
      name: '孙子、孙女或外孙子、外孙女',
    },
    {
      code: '5',
      name: '父母',
    },
    {
      code: '6',
      name: '祖父母或外祖父母',
    },
    {
      code: '7',
      name: '兄、弟、姐、妹',
    },
    {
      code: '8',
      name: '家庭内其他关系',
    },
    {
      code: '9',
      name: '非家庭关系成员',
    },
  ],
  identity: [
    {
      code: '市直医保',
      name: '市直医保',
    },
    {
      code: '省直门特',
      name: '省直门特',
    },
    {
      code: '市直门特',
      name: '市直门特',
    },
    {
      code: '自费',
      name: '自费',
    },
    {
      code: '省内异地',
      name: '省内异地',
    },
    {
      code: '广州医保',
      name: '广州医保',
    },
    {
      code: '门特一类',
      name: '门特一类',
    },
    {
      code: '门特二类',
      name: '门特二类',
    },
    {
      code: '生育保险',
      name: '生育保险',
    },
    {
      code: '居民医保',
      name: '居民医保',
    },
    {
      code: '门诊统筹',
      name: '门诊统筹',
    },
    {
      code: '单位全免',
      name: '单位全免',
    },
    {
      code: '外省农合',
      name: '外省农合',
    },
    {
      code: '省工伤',
      name: '省工伤',
    },
    {
      code: '特约单位',
      name: '特约单位',
    },
    {
      code: '单位免号',
      name: '单位免号',
    },
    {
      code: '新农合',
      name: '新农合',
    },
    {
      code: '跨省异地',
      name: '跨省异地',
    },
    {
      code: '公费0%',
      name: '公费0%',
    },
    {
      code: '公费5%',
      name: '公费5%',
    },
    {
      code: '公费10%',
      name: '公费10%',
    },
    {
      code: '公费20%',
      name: '公费20%',
    },
    {
      code: '公费50%',
      name: '公费50%',
    },
    {
      code: '广州工伤',
      name: '广州工伤',
    },
    {
      code: '市公医',
      name: '市公医',
    },
    {
      code: '增城公医',
      name: '增城公医',
    },
    {
      code: '市公门特',
      name: '市公门特',
    },
    {
      code: '外地医保',
      name: '外地医保',
    },
  ],
  nativePlace: [
    {
      code: '110000',
      name: '北京市',
    },
    {
      code: '110100',
      name: '北京市辖区',
    },
    {
      code: '110101',
      name: '北京东城区',
    },
    {
      code: '110102',
      name: '北京西城区',
    },
  ],
  patSource: [
    {
      code: '1',
      name: '医院所在区县',
    },
    {
      code: '2',
      name: '本市其他区县',
    },
    {
      code: '3',
      name: '本省其他市',
    },
    {
      code: '4',
      name: '外省市',
    },
    {
      code: '5',
      name: '港澳台地区',
    },
    {
      code: '6',
      name: '外国',
    },
  ],
}; //存字典

const docAdmiNote = ref({} as IgetDetailByNoticeSerialNo.DocAdmiNote);

const goStartRegister = () => {
  router.push({ path: 'startRegister', query: {} });
};
interface IGetters {
  getters: () => IgetDetailByNoticeSerialNo.DocAdmiNote;
}

const handleProvCityCounty = (resData: commonTypes.ISieDictionaries[]) => {
  let provsData: any = [];
  let citysData = {};
  let allCitys: any = [];
  let distsData = {};

  resData.forEach((v) => {
    v.text = v.itemsName;
    v.value = v.itemsCode;
    if (v.upperCode === '0') {
      provsData.push(v);
    }
  });

  //省
  provsData.forEach((provs: commonTypes.ISieDictionaries) => {
    let citys = resData.filter((v) => v.upperCode === provs.itemsCode);
    citys.forEach((i) => {
      i.children = [];
    });
    citysData[provs.itemsCode as string] = citys;
    allCitys = allCitys.concat(citys);
    provs.children = [];
  });

  allCitys.forEach((citys: any) => {
    let dists = resData.filter((v) => v.upperCode === citys.itemsCode);
    distsData[citys.itemsCode] = dists;
  });
  let areaData = {
    provsData,
    citysData,
    distsData,
  };

  const columns = [
    /*{
      text: '浙江',
      children: [
        {
          text: '杭州',
          children: [{ text: '西湖区' }, { text: '余杭区' }],
        },
        {
          text: '温州',
          children: [{ text: '鹿城区' }, { text: '瓯海区' }],
        },
      ],
    }*/
  ];
  for (const key in citysData) {
    if (citysData[key] && citysData[key].length) {
      citysData[key].forEach((i: commonTypes.ISieDictionaries) => {
        for (const distsKey in distsData) {
          if (i.itemsCode == distsKey) {
            i.children = distsData[distsKey];
          }
        }
      });
    }
  }
  provsData.forEach((i: commonTypes.ISieDictionaries) => {
    for (const key in citysData) {
      if (i.itemsCode == key) {
        i.children = citysData[key];
      }
    }
  });

  console.log('处理后省市区：', areaData);
};

//获取省市区
const getProvCityCountyFn = async () => {
  const toast = Toast.loading({
    overlay: true,
    duration: 0,
  });
  let hospitalId = store.state.hospitalId;
  const [err, res] = await useTo(getProvCityCounty(hospitalId));
  toast.clear();
  if (err) return Toast(err.errorMsg);
  //handleProvCityCounty(res.busObj);
  store.dispatch('provCityCountyAction', res.busObj);

  store.dispatch('handleProvCityCountyAction', res.busObj); //放到异步里面

  //console.log('省市区：', res.busObj);
};
//获取字典
const queryDictionariesFn = async () => {
  const toast = Toast.loading({
    overlay: true,
    duration: 0,
  });
  let hospitalId = store.state.hospitalId;
  const [err, res] = await useTo(queryDictionaries(hospitalId));
  toast.clear();
  if (err) return Toast(err.errorMsg);
  store.dispatch('dictionariesAction', res.busObj);
  console.log('字典：', res.busObj);
};

const getDetailByNoticeSerialNoFn = async () => {
  const toast = Toast.loading({
    overlay: true,
    duration: 0,
  });
  const [err, res] = await useTo(getDetailByNoticeSerialNo(route.query.noticeSerialNo ?? null));
  toast.clear();
  if (err) return Toast(err.errorMsg);
  let obj: IgetDetailByNoticeSerialNo.BusObj = res.busObj.busObj;

  if (obj.patAdmiNote && !isEmpty(obj.patAdmiNote)) {
    return router.push({
      path: 'info',
      query: { noticeSerialNo: route.query.noticeSerialNo ?? null },
    });
  } else {
    obj.patAdmiNote = {} as IgetDetailByNoticeSerialNo.PatAdmiNote;
  }

  store.dispatch('IsConfirmAction', res.busObj.isConfirm);
  store.dispatch('formAction', obj);

  docAdmiNote.value = obj.docAdmiNote as IgetDetailByNoticeSerialNo.DocAdmiNote;
  console.log('详情：', obj);
};

//模拟数据
const mock = () => {
  let patientName = store.state.form.patAdmiNote?.patientName ?? null; //store.state.form.patAdmiNote.patientName ?? null;
  if (!patientName) {
    store.dispatch('IsConfirmAction', formPam.isConfirm);
    store.dispatch('formAction', formPam.busObj); //获取到接口的
  }
  const getFormDocAdmiNote = computed(
    (): IgetDetailByNoticeSerialNo.DocAdmiNote => store.getters.getFormDocAdmiNote,
  );
  docAdmiNote.value = getFormDocAdmiNote.value;
};

onMounted(() => {
  //console.log('onMounted:', process.env.NODE_ENV);
  queryDictionariesFn();
  getProvCityCountyFn();
  getDetailByNoticeSerialNoFn();
  // mock();
});

//watchEffect 会最先执行
watchEffect(() => {
  //console.log('watchEffect:', 3333);
});
</script>

<style scoped lang="scss">
.continue {
  .wrap {
  }
}
</style>
